package com.wy.springbootblog.controller;

import com.wy.springbootblog.pojo.Article;
import com.wy.springbootblog.pojo.Result;
import com.wy.springbootblog.pojo.SearchBean;
import com.wy.springbootblog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    // 新增文章
    @PostMapping("/add")
    public Result<Void> add(@RequestBody @Validated Article article) {
        return articleService.saveArticle(article);
    }

    // 删除文章
    @DeleteMapping("/delete/{id}")
    public Result<Void> delete(@PathVariable @NotNull(message = "文章id不能为空") Integer id) {
        return articleService.deleteArticle(id);
    }

    // 更新文章
    @PostMapping("/update")
    public Result<Void> update(@RequestBody @Validated Article article) {
        return articleService.updateArticle(article);
    }

    // 查询文章列表
    @GetMapping("/list")
    public Result<List<Article>> list(@RequestBody SearchBean<Article> searchBean) {
        return articleService.articleList(searchBean);
    }

    // 查询文章详情
    @GetMapping("/detail/{id}")
    public Result<Article> detail(@PathVariable @NotNull(message = "文章id不能未空") Integer id) {
        // 1.增加博客详情浏览器
        articleService.increaseViews(id);
        return articleService.articleDetail(id);
    }
}
